package com.bdqn.t320.util;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Aspect
@Component
public class LogAspect {

    /**
     * 环绕切入带有@MyLog注解的方法
     *
     * @param point
     * @param myLog
     * @return
     */
    @Around("@annotation(myLog)")
    public Object around(ProceedingJoinPoint point, MyLog myLog) throws Throwable {
        //获取目标class对象
        Class<?> aClass = point.getTarget().getClass();
        //获取类名
        String className = aClass.getName();
        //获取目标方法名称
        String methodName = point.getSignature().getName();
        //获取mylog注解参数
        String value = myLog.value();
        Object[] args = point.getArgs();
        StringBuffer stringBuffer = new StringBuffer();
        for (Object arg : args) {
            stringBuffer.append(arg).append("\\");
        }
        UUID uuid = UUID.randomUUID();
        System.out.printf("Mylog%s,类名[%s],方法名[%s],注解值[%s],方法参数[%s]\n",uuid,className,methodName,value,stringBuffer);
        long startTime = System.currentTimeMillis();
        Object proceed = point.proceed();
        long endTime = System.currentTimeMillis();
        System.out.printf("Mylog%s,类名[%s],方法名[%s],注解值[%s],方法耗时[%s],返回值[%s]\n",uuid,className,methodName,stringBuffer,endTime-startTime,proceed);
        return proceed;
    }
}
